﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.admin
{
    public partial class ManageDept : System.Web.UI.Page
    {
        public DeptDAL dal = new DeptDAL();
        public LogsDAL logs = new LogsDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindDept();
            }
        }

        public void BindDept()
        {
            this.gvDept.DataSource = dal.GetAllDept();
            this.gvDept.DataBind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string DeptName = this.TextBoxNames.Text;
            
            int temp = dal.AddDept(DeptName);
            if (temp > 0)
            {
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("insert new department", userInfo.UserName, Ip);

                this.BindDept();

            }
        }

        protected void gvDept_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvDept.EditIndex = e.NewEditIndex;
            this.BindDept();
        }

        protected void gvDept_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvDept.EditIndex = -1;
            this.BindDept();

        }

        protected void gvDept_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int DeptId = int.Parse(this.gvDept.DataKeys[e.RowIndex].Values[0].ToString());
            string DeptName = ((TextBox)(this.gvDept.Rows[e.RowIndex].Cells[1].FindControl("txtName"))).Text;
           
            int temp = dal.UpdateDept(DeptId, DeptName);
            if (temp > 0)
            {
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("update department", userInfo.UserName, Ip);
                this.gvDept.EditIndex = -1;
                this.BindDept();

            }
        }

        protected void gvDept_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int DeptId = int.Parse(this.gvDept.DataKeys[e.RowIndex].Values[0].ToString());

            int temp = dal.DeleteDept(DeptId);
            if (temp > 0)
            {
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("delete department", userInfo.UserName, Ip);

                this.BindDept();

            }
        }

        protected void gvDept_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
                }
            }

        }
    }
}

